package com.vincent.template.service.logic.impl;

import com.google.common.collect.Lists;
import com.vincent.template.commons.exception.Vct520Exception;
import com.vincent.template.domain.SysUserGroupPo;
import com.vincent.template.dto.sys.usergroup.UserGroupDto;
import com.vincent.template.repository.SysBindUserGroupRoleRepository;
import com.vincent.template.repository.SysBindUserGroupUserRepository;
import com.vincent.template.repository.SysUserGroupRepository;
import com.vincent.template.service.logic.UserGroupService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Vincent Wang
 * @date 2021/3/25 17:01
 */
@Service
public class UserGroupServiceImpl implements UserGroupService {

    @Autowired
    private SysUserGroupRepository userGroupRepository;
    @Autowired
    private SysBindUserGroupUserRepository bindUserGroupUserRepository;
    @Autowired
    private SysBindUserGroupRoleRepository bindUserGroupRoleRepository;

    @Override
    public List<UserGroupDto> findUserGroups() {
        List<SysUserGroupPo> userGroups = userGroupRepository.findAll();
        if (CollectionUtils.isEmpty(userGroups)) {
            return Lists.newArrayList();
        }
        return userGroups.stream().map(group -> {
            UserGroupDto userGroupDto = new UserGroupDto();
            BeanUtils.copyProperties(group, userGroupDto);
            return userGroupDto;
        }).collect(Collectors.toList());
    }

    @Override
    public UserGroupDto findUserGroupById(Long id) {
        SysUserGroupPo userGroup = userGroupRepository.findById(id).orElse(null);
        if (userGroup == null) {
            return null;
        }
        UserGroupDto userGroupDto = new UserGroupDto();
        BeanUtils.copyProperties(userGroup, userGroupDto);
        return userGroupDto;
    }

    @Override
    public Long addUserGroup(UserGroupDto groupDto) {
        SysUserGroupPo userGroup = userGroupRepository.findByGroupName(groupDto.getGroupName());
        Vct520Exception.throwIfTrue(userGroup != null,
                "该用户组名已存在：" + groupDto.getGroupName());
        userGroup = new SysUserGroupPo();
        BeanUtils.copyProperties(groupDto, userGroup);
        return userGroupRepository.save(userGroup).getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateUserGroup(Long id, UserGroupDto groupDto) {
        SysUserGroupPo userGroup = userGroupRepository.findById(id).orElse(null);
        Vct520Exception.throwIfEmpty(userGroup, "用户组不存在");
        if (!userGroup.getGroupName().equals(groupDto.getGroupName())) {
            Vct520Exception.throwIfTrue(userGroupRepository.findByGroupName(groupDto.getGroupName()) != null,
                    "该用户组名已存在：" + groupDto.getGroupName());
        }
        BeanUtils.copyProperties(groupDto, userGroup, "id");
        userGroupRepository.save(userGroup);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteUserGroup(Long id) {
        SysUserGroupPo userGroup = userGroupRepository.findById(id).orElse(null);
        Vct520Exception.throwIfEmpty(userGroup, "用户组不存在");
        Vct520Exception.throwIfTrue(CollectionUtils.isNotEmpty(bindUserGroupUserRepository.findByUserGroupId(id)),
                "有用户绑定了该用户组，请先确认并解绑");
        userGroupRepository.delete(userGroup);
        bindUserGroupRoleRepository.deleteByUserGroupId(id);
        return true;
    }
}
